﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace CustomControlLib
{
    public class ToggleSwitch:CheckBox
    {
        static ToggleSwitch()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleSwitch), new FrameworkPropertyMetadata(typeof(ToggleSwitch)));
        }

        public ToggleSwitch()
        {
            Checked += ToggleSwitch_Checked;

            Unchecked += ToggleSwitch_Unchecked;
        }
        private void ToggleSwitch_Checked(object sender, RoutedEventArgs e)
        {
            Animatebutton(true);
        }
        private void ToggleSwitch_Unchecked(object sender, RoutedEventArgs e)
        {
            Animatebutton(false);
        }

        /// <summary>
        /// 确保按钮模板全部加载完成
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            // 注册 LayoutUpdated 事件
            LayoutUpdated += ToggleSwitch_LayoutUpdated;
        }
        /// <summary>
        /// 确保控件全部修饰完成，能正确获取实际 ActualWidth 与 ActualHeight
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToggleSwitch_LayoutUpdated(object sender, EventArgs e)
        {
            // 确保 ActualWidth 和 ActualHeight 已经计算完毕
            if (ActualWidth > 0 && ActualHeight > 0)
            {
                // 移除 LayoutUpdated 事件处理程序，防止多次触发
                LayoutUpdated -= ToggleSwitch_LayoutUpdated;

                // 如果控件初始化时 IsChecked 为 true，立即触发动画
                if (IsChecked == true)
                {
                    Animatebutton(true);
                }
            }
        }

        private void Animatebutton(bool isChecked)
        {
            if (GetTemplateChild("button") is Border button)
            {
                Storyboard storyboard = new Storyboard();
                ThicknessAnimation animation = new ThicknessAnimation();
                animation.Duration=new Duration (TimeSpan.FromSeconds(0.3));
                animation.EasingFunction=new CircleEase { EasingMode = EasingMode.EaseOut };

                if (isChecked)
                {
                    animation.From = new Thickness(4, 0, 0, 0);
                    animation.To=new Thickness(ActualWidth-(ActualHeight-8)-4, 0, 0, 0);
                }
                else
                {
                    animation.To = new Thickness(4, 0, 0, 0);
                    animation.From = new Thickness(ActualWidth - (ActualHeight - 8) - 4, 0, 0, 0);
                }

                Storyboard.SetTarget(animation,button); 
                Storyboard.SetTargetProperty(animation,new PropertyPath(MarginProperty));
                storyboard.Children.Add(animation);

                if (isChecked)
                {
                    Resources.Remove("Left");
                    Resources["Right"] = storyboard;
                }
                else
                {
                    Resources.Remove("Right");
                    Resources["Left"] = storyboard;
                }
                storyboard.Begin();
            }
        }
      
    }
}
